package cn.cdeden.common.elasticsearch.index;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;

/**
 * @ProjectName : KnowCore
 * @Package : cn.cdeden.common.elasticsearch.index
 * @ClassName : EsIndexesInfo
 * @Author : Yu.
 * @Description : // TODO
 * @Date : 2024/12/12 20:09
 * @Version : 1.0
 */
@Slf4j
public class EsIndexesInfo {


    public static String getIndexName(String IndexName) {
//        String indexName = "classpath:META-INF/esIndex/"+IndexName+".json";
        String indexName = "META-INF/esIndex/"+IndexName+".json";
//        相对于当前类所在的包路径下获取资源
        String string = null;
        try {
            string = loadFileContent(indexName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return string;
    }
    private static String loadFileContent(String filename) throws Exception {
        Resource resource = new ClassPathResource(filename);
        try (InputStream inputStream = resource.getInputStream()) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            return reader.lines().collect(Collectors.joining(System.lineSeparator()));
        }
    }

    public static final  String  MAPPING_TEMPLATE = "{\n" +
        "  \"mappings\": {\n" +
        "    \"properties\": {\n" +
        "      \"attachment\": {\n" +
        "        \"properties\": {\n" +
        "          \"content\": {\n" +
        "            \"type\": \"text\",\n" +
        "            \"analyzer\": \"ik_max_word\",\n" +
        "            \"search_analyzer\": \"ik_smart\",\n" +
        "            \"fields\": {\n" +
        "              \"keyword\": {\n" +
        "                \"type\": \"keyword\",\n" +
        "                \"ignore_above\": 256\n" +
        "              }\n" +
        "            }\n" +
        "          },\n" +
        "          \"content_length\": {\n" +
        "            \"type\": \"long\"\n" +
        "          },\n" +
        "          \"content_type\": {\n" +
        "            \"type\": \"text\",\n" +
        "            \"fields\": {\n" +
        "              \"keyword\": {\n" +
        "                \"type\": \"keyword\",\n" +
        "                \"ignore_above\": 256\n" +
        "              }\n" +
        "            }\n" +
        "          },\n" +
        "          \"language\": {\n" +
        "            \"type\": \"text\",\n" +
        "            \"fields\": {\n" +
        "              \"keyword\": {\n" +
        "                \"type\": \"keyword\",\n" +
        "                \"ignore_above\": 256\n" +
        "              }\n" +
        "            }\n" +
        "          }\n" +
        "        }\n" +
        "      },\n" +
        "      \"createBy\": {\n" +
        "        \"type\": \"long\"\n" +
        "      },\n" +
        "      \"createDept\": {\n" +
        "        \"type\": \"long\"\n" +
        "      },\n" +
        "      \"createTime\": {\n" +
        "        \"type\": \"date\"\n" +
        "      },\n" +
        "      \"fileSuffix\": {\n" +
        "        \"type\": \"text\"\n" +
        "      },\n" +
        "      \"id\": {\n" +
        "        \"type\": \"text\"\n" +
        "      },\n" +
        "      \"moduleType\": {\n" +
        "        \"type\": \"text\"\n" +
        "      },\n" +
        "      \"ossId\": {\n" +
        "        \"type\": \"text\"\n" +
        "      },\n" +
        "      \"title\": {\n" +
        "        \"type\": \"text\",\n" +
        "        \"analyzer\": \"ik_max_word\",\n" +
        "        \"search_analyzer\": \"ik_smart\",\n" +
        "        \"fields\": {\n" +
        "          \"keyword\": {\n" +
        "            \"type\": \"keyword\",\n" +
        "            \"ignore_above\": 256\n" +
        "          }\n" +
        "        }\n" +
        "      },\n" +
        "      \"type\": {\n" +
        "        \"type\": \"text\"\n" +
        "      },\n" +
        "      \"updateBy\": {\n" +
        "        \"type\": \"long\"\n" +
        "      },\n" +
        "      \"updateTime\": {\n" +
        "        \"type\": \"date\"\n" +
        "      },\n" +
        "      \"versionId\": {\n" +
        "        \"type\": \"text\"\n" +
        "      },\n" +
        "      \"versionName\": {\n" +
        "        \"type\": \"text\"\n" +
        "      }\n" +
        "    }\n" +
        "  }\n" +
        "}";


}
